/*
	       File: ZipFile.cs
	Create Date: 2007-09-11
	    Version:

	Description:


	 Change log:


*/
using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
using System.Drawing;
using System.IO;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Design;
using System.Workflow.ComponentModel.Compiler;
using System.Workflow.ComponentModel.Serialization;
using System.Workflow.Runtime;
using System.Workflow.Activities;
using System.Workflow.Activities.Rules;

using GuerillaProgrammer.Components;
using GuerillaProgrammer.Components.Logging;
using GuerillaProgrammer.FileProcessor;
using GuerillaProgrammer.FileProcessor.Settings;
using GuerillaProgrammer.FileProcessor.Zip;

namespace GuerillaProgrammer.FileProcessor.Activities
{
    public partial class ZipFile : Activity
    {
        #region Private member fields
        private bool _Overwrite;
        private string _SourceFile;
        private string _DestinationFile;
        #endregion

        #region Constructors and cleanup methods
        public ZipFile()
        {
            InitializeComponent();
        }
        #endregion

        #region Private implementation methods
        #endregion

        #region Class properties
        public static DependencyProperty OverwriteProperty = System.Workflow.ComponentModel.DependencyProperty.Register("Overwrite", typeof(bool), typeof(ZipFile));
        [Description("Overwrite the zip file if it exists")]
        [Category("Zip File")]
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public bool Overwrite
        {
            get { return _Overwrite; }
            set { _Overwrite = value; }
        }
        public static DependencyProperty SourceFileProperty = System.Workflow.ComponentModel.DependencyProperty.Register("SourceFile", typeof(string), typeof(ZipFile));
        [Description("Path to the source file")]
        [Category("Zip File")]
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public string SourceFile
        {
            get { return _SourceFile; }
            set { _SourceFile = value; }
        }
        public static DependencyProperty DestinationFileProperty = System.Workflow.ComponentModel.DependencyProperty.Register("DestinationFile", typeof(string), typeof(ZipFile));
        [Description("Path to the destination file")]
        [Category("Zip File")]
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public string DestinationFile
        {
            get { return _DestinationFile; }
            set { _DestinationFile = value; }
        }
        #endregion

        #region Events handlers
        protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
        {
            Logger.LogEnter();
            IProcessorDataService dataService = executionContext.GetService(typeof(IProcessorDataService)) as IProcessorDataService;
            if (null == dataService)
            {
                throw new InvalidOperationException("Can't get the service!");
            }

            try
            {
                WorkItem workItem = dataService.GetWorkItem();
                StringProcessor stringProcessor = new StringProcessor(new FileProcessorSymbolTable(dataService.GetConfiguration(), workItem));

                string processedSource = stringProcessor.SubstituteStringValues(_SourceFile);
                string processedDestination = stringProcessor.SubstituteStringValues(_DestinationFile);

                Logger.LogInfo("ZipFile", "Compressing file {0} to {1}\n", processedSource, processedDestination);
                if ((true == _Overwrite) && (true == File.Exists(processedDestination)))
                {
                    Logger.LogInfo("ZipFile", "The destination file ({0}) already exists. it will be deleted.", processedDestination);
                    File.Delete(processedDestination);
                }

                GuerillaProgrammer.FileProcessor.Zip.ZipFile zipFile = new GuerillaProgrammer.FileProcessor.Zip.ZipFile(processedDestination);
                zipFile.AddFile(processedSource);
                zipFile.Save();
            }
            catch (Exception exc)
            {
                Logger.LogError("FileMove", "An error occured while attempting to move a file.\nError: {0}", exc.Message);
                throw exc;
            }
            Logger.LogLeave();
            return base.Execute(executionContext);
        }
        #endregion
	}
}
